package config

import (
	"github.com/elastic/go-elasticsearch/v7"
	"log"
)

var (
	Es *elasticsearch.Client
)

func InitElasticsearch() {
	cfg := elasticsearch.Config{
		Addresses: []string{
			"http://localhost:9200", // 你的ES地址
		},
		// 7.x 版本的其他配置...
	}

	es, err := elasticsearch.NewClient(cfg)
	if err != nil {
		log.Fatalf("Error creating the client: %s", err)
	}

	// 测试连接
	res, err := es.Info()
	if err != nil {
		log.Fatalf("Error getting response: %s", err)
	}
	defer res.Body.Close()

	if res.IsError() {
		log.Fatalf("Error: %s", res.String())
	}

	log.Println("Successfully connected to Elasticsearch 7.x")
	Es = es
}
